/*******************************************************************************
* Project: QSAR(Cute Software Architecture) 
*
* Copyright: Copyright(C) 2024 by YuetingBen, All Rights Reserved
*
* File name: CDD_24C02.c
*
* Brief: ;
*
* Author: Yueting.Ben
*
* Module: CDD_24C02
*
*******************************************************************************/

/** HEADER FILES **/
#include "CDD_24C02.h"


/** MACRO DEFINITION **/


/** TYPEDEF DEFINITION **/


/** LOCAL DATA DECLARATION **/


/** GLOBAL DATA DECLARATION **/


/** LOCAL FUNCTION DECLARATION **/


/** GLOBAL FUNCTION DECLARATION **/
void CDD_24C02_GetVersionInfo(
    Std_VersionInfoType* versionInfo
);

Std_ReturnType CDD_24C02_MemRead(
    uint8 instanceId,
    uint32 sourceAddress,
    uint8* destinationDataPtr,
    uint32 length
);

Std_ReturnType CDD_24C02_MemWrite(
    uint8 instanceId,
    uint32 targetAddress,
    const uint8* sourceDataPtr,
    uint32 length
);

/** LOCAL DATA **/
static void CDD_24C02_DataRead(
    uint16 MemAddress,  
    uint8 *pData, 
    uint16 Size
);

static void CDD_24C02_DataWrite(
    uint16 MemAddress,  
    uint8 *pData, 
    uint16 Size
);

/** GLOBAL DATA **/


/** LOCAL FUNCTION **/

/*******************************************************************************
* -Name            CDD_24C02_DataRead
* -Brief           Copy the from the source address into the referenced destination data buffer
* -Details         None
* -Param[in]       MemAddress: Physical address to read data from
* -Param[in]       Size: Read length in bytes
* -Param[out]      pData: Pointer to store the read data
* -Return          None          
*******************************************************************************/
static void CDD_24C02_DataRead(
    uint16 MemAddress,  
    uint8* pData, 
    uint16 Size
)
{
    while(HAL_OK != HAL_I2C_Mem_Read(&hi2c2, 0xA1, MemAddress, I2C_MEMADD_SIZE_8BIT, pData, Size, 0xFF));
}

/*******************************************************************************
* -Name            CDD_24C02_DataWrite
* -Brief           Store the passed data to the provided address area with given address and length
* -Details         None
* -Param[in]       MemAddress: Physical write address
* -Param[in]       Size: Write length in bytes
* -Param[out]      pData: Pointer to store the write data
* -Return          None          
*******************************************************************************/
static void CDD_24C02_DataWrite(
    uint16 MemAddress,  
    uint8* pData, 
    uint16 Size
)
{
    uint8 cycleTemp = 0u;

    while(8 * (cycleTemp + 1)  < Size)
    {
        while(HAL_OK != HAL_I2C_Mem_Write(&hi2c2, 0xA0, (MemAddress + (cycleTemp * 8u)), I2C_MEMADD_SIZE_8BIT, pData, 8u, 0xFFF));
        pData = pData + 8u;
        cycleTemp++;
    }
    while(HAL_OK != HAL_I2C_Mem_Write(&hi2c2, 0xA0, (MemAddress + (cycleTemp * 8u)), I2C_MEMADD_SIZE_8BIT, pData, (Size - (cycleTemp * 8u)), 0xFFF));
}


/** GLOBAL FUNCTION **/

/*******************************************************************************
* -Name            CDD_24C02_GetVersionInfo
* -Brief           Returns the version information of this module 
* -Details         None
* -Param[out]      versioninfo: Pointer to the version information of this module
* -Return          None          
*******************************************************************************/
void CDD_24C02_GetVersionInfo(
    Std_VersionInfoType* versionInfo
)
{
    versionInfo->vendorID = (uint16)YSAR_VENDOR_ID;
    versionInfo->moduleID = (uint8)CDD_24C02_MODULE_ID;
    versionInfo->sw_major_version = (uint8)CDD_24C02_SW_MAJOR_VERSION;
    versionInfo->sw_minor_version = (uint8)CDD_24C02_SW_MINOR_VERSION;
    versionInfo->sw_patch_version = (uint8)CDD_24C02_SW_PATCH_VERSION;
}


/*******************************************************************************
* -Name            CDD_24C02_MemRead
* -Brief           Triggers a read job to copy the from the source address into the referenced destination data buffer 
* -Details         None
* -Param[in]       instanceId: ID of the related memory driver instance
* -Param[in]       sourceAddress: Physical address to read data from
* -Param[in]       length: Read length in bytes
* -Param[out]      destinationDataPtr: Destination memory pointer to store the read data
* -Return          E_OK: The requested job has been accepted by the module
                   E_NOT_OK: The requested job has not been accepted by the module
                   E_MEM_SERVICE_NOT_AVAIL: The service function is not implemented
*******************************************************************************/
Std_ReturnType CDD_24C02_MemRead(
    uint8 instanceId,
    uint32 sourceAddress,
    uint8* destinationDataPtr,
    uint32 length
)
{
    CDD_24C02_DataRead((uint16)sourceAddress, (uint8*)destinationDataPtr, (uint16)length);
}

/*******************************************************************************
* -Name            CDD_24C02_MemWrite
* -Brief           Triggers a write job to store the passed data to the provided address area with given address and length 
* -Details         None
* -Param[in]       instanceId: ID of the related memory driver instance
* -Param[in]       targetAddress: Physical write address (aligned to page size)
* -Param[in]       sourceDataPtr: Source data pointer (aligned to page size)
* -Param[in]       length: Write length in bytes (aligned to page size)
* -Return          E_OK: The requested job has been accepted by the module
                   E_NOT_OK: The requested job has not been accepted by the module
                   E_MEM_SERVICE_NOT_AVAIL: The service function is not implemented
*******************************************************************************/
Std_ReturnType CDD_24C02_MemWrite(
    uint8 instanceId,
    uint32 targetAddress,
    const uint8* sourceDataPtr,
    uint32 length
)
{
    CDD_24C02_DataWrite((uint16)targetAddress, (uint8*)sourceDataPtr, (uint16)length);
}

